<?php
require_once dirname(__FILE__) . '/IPreprocess.php';

require_once DEF_ROOT . 'TextDef.php';
require_once DATA_ROOT . 'TextData.php';
class TextPreprocess implements IPreprocess
{
    public static function getContent($content)
    {
        $arr_cond = array('tid' =>$content, 'type' => TextType::NORMAL);
        $ret = TextData::model()->findData();
        if (!empty($ret)) {
            return $ret[0];
        } else {
            return null;
        }
    }
    
    public static function processContent($content)
    {
        $ret = TextData::addData($content, TextType::NORMAL);
        return $ret->tid;
    }
    
    public static function editContent($content, $id)
    {
        $arr_cond = array('tid' =>$id, 'type' => TextType::NORMAL);
        $ret = TextData::model()->findData();
        if (empty($ret))
        {
            throw new Exception('Text for id ' . $id . 'is not exist', ErrorDef::COMMON_ERROR_NOT_EXIST);
        }
        
        $textData = $ret[0];
        
        $textData->editData($content);
        return $content;
    }
}